<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\GuildHostOp;
use App\Http\Controllers\Controller;
use App\Model\Guild\Guild;
use App\Model\Guild\GuildHost;
use App\Model\Stat\StatDailyHostInfo;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Model\WhitePhone;
use App\Util\InfoUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

/**
 * 活动管理
 */
class ActivitiesController extends Controller
{
    public function videoRank()
    {
        $createdAt = request('created_at', date('Y-m-d'));

        $mondayTs = (strtotime($createdAt) - ((date('w', strtotime($createdAt)) == 0 ? 7 : date('w', strtotime($createdAt))) - 1) * 24 * 3600);
        $sundayTs = (strtotime($createdAt) + (date('w', strtotime($createdAt)) == 0 ? 0 : (7 - date('w', strtotime($createdAt)))) * 24 * 3600);

        $mondayTs = date('Y-m-d', $mondayTs);
        $sundayTs = date('Y-m-d', $sundayTs);

        $userIds = StatDailyHostInfo::getCallRankList(30, $mondayTs, $sundayTs);

        $ranking = [];
        foreach ($userIds as $key => $item) {
            $userProfile = UserProfile::getProfile($item['user_id']);

            $ranking[] = [
                'ranking' => ++$key,
                'user_id' => $userProfile->user_profile_id,
                'call_minutes' => $item['call_minutes'],
                'nick_name' => $userProfile->nick_name,
                'date' => "$mondayTs ~ $sundayTs"
            ];
        }

        return view('video_rank', ['info' => $ranking]);
    }
}
